package com.wjh.util;

import java.security.MessageDigest;
import java.util.Random;

import lombok.extern.slf4j.Slf4j;

/**
 * md5加密工具类
 * 
 * @author wenjianhai
 * @date 2023/6/10
 * @since JDK 17
 */
@Slf4j
public class Md5Utils {
	public static String encrypt(String strSrc) {
		try {
			char hexChars[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
			byte[] bytes = strSrc.getBytes();
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(bytes);
			bytes = md.digest();
			int j = bytes.length;
			char[] chars = new char[j * 2];
			int k = 0;
			for (int i = 0; i < bytes.length; i++) {
				byte b = bytes[i];
				chars[k++] = hexChars[b >>> 4 & 0xf];
				chars[k++] = hexChars[b & 0xf];
			}
			return new String(chars);
		} catch (Exception e) {
			log.error("MD5加密-失败:" + e.getMessage(), e);
			throw new RuntimeException("MD5加密出错！！+" + e);
		}
	}

	/**
	 * 生成密钥
	 *
	 * @date 2023-06-10
	 * @return String
	 * @since JDK 17
	 * @author wenjianhai
	 */
	public static String getSignKey() {
		Random random = new Random();
		return encrypt(System.currentTimeMillis() + String.valueOf(random.nextInt(1000)));
	}
}
